#!/bin/bash
item=SDL
prefix=../../output/arm_linux
host=arm-linux
build=i386-linux
tmp=tmp123456
tsprefix=../../$item/output/arm_linux

#cd libs &&
#./tar "./*.gz" "." &&
#./tar "./*.bz2" "." &&
if [ ! -e output/arm_linux ]
then
	mkdir -p output/arm_linux
fi

mkdir ../$tmp &&
cp libs/tslib-1.4.tar.gz ../$tmp/ &&
cd ../$tmp &&
tar -xvzf tslib-1.4.tar.gz &&
cd tslib &&
./autogen.sh &&
./configure --host=$host --prefix=$PWD/$tsprefix ac_cv_func_malloc_0_nonnull=yes &&
make clean && make && make install &&
cd ../../$item/libs &&
sed -i -e '1,5s/# module_raw input/module_raw input/' ../output/arm_linux/etc/ts.conf  &&

tar -xvzf libiconv-1.13.1.tar.gz &&
cd libiconv-1.13.1 &&
./configure --prefix=$PWD/$prefix --host=$host  &&
make clean && make && make install && cd .. && 

tar -xvzf SDL-1.2.14.tar.gz &&
cd SDL-1.2.14 &&
./configure --prefix=$PWD/$prefix --disable-video-nanox --disable-video-qtopia --disable-video-photon --disable-video-ggi --disable-video-svga --disable-video-directfb --disable-video-aalib --disable-video-dummy --disable-video-dga --disable-arts --disable-esd --disable-alsa --disable-video-x11 --disable-nasm --disable-joystick --enable-input-tslib --enable-video-fbcon --host=$host --build=i386  CFLAGS="-I$PWD/$prefix/include" LDFLAGS="-L$PWD/$prefix/lib -lts -liconv"
make clean && make && make install &&

cd test &&
NM=arm-linux-nm LD=arm-linux-ld CC=arm-linux-gcc CXX=arm-linux-g++ AR=arm-linux-ar ./configure --host=$host --prefix=$PWD/../$prefix &&
sed -i -e '8,13s/testgl$(EXE) testhread/testhread/' Makefile &&
sed -i -e '8,13s/testjoystick$(EXE) testkeys/testkeys/' Makefile &&
make clean && make &&
cp icon.bmp sample.bmp sail.bmp graywin loopwave testalpha testbitmap testblitspeed testcdrom testcursor testdyngl testerror testfile testgamma testhread testiconv testkeys testloadso testlock testoverlay testoverlay2 testpalette testplatform testsem testsprite testtimer testver testvidinfo testwin threadwin torturethread  $PWD/../$prefix/bin -dup &&
cd ../.. &&  

tar -xvzf jpegsrc.v6b.tar.gz &&
cd jpeg-6b &&
mkdir -p $prefix/man/man1 &&
./configure --prefix=$PWD/$prefix --host=$host  --enable-shared --enable-static &&
sed -i -e '8,75s/CC= gcc/CC= arm-linux-gcc/' Makefile &&
sed -i -e '8,75s/AR= ar rc/AR= arm-linux-ar rc/' Makefile &&
sed -i -e '8,75s/AR2= ranlib/AR2= arm-linux-ranlib/' Makefile &&
make clean && make && make install &&
cd .. &&


tar -xvjf libpng-1.2.19.tar.bz2 &&
cd libpng-1.2.19 &&
./configure --prefix=$PWD/$prefix --host=$host  --enable-shared --enable-static &&
make clean && make && make install &&
cd .. &&

tar -xvzf SDL_image-1.2.8.tar.gz &&
cd SDL_image-1.2.8 &&
./configure --prefix=$PWD/$prefix --host=$host  CFLAGS="-I$PWD/$prefix/include" LDFLAGS="-L$PWD/$prefix/lib -ljpeg -lpng12 -lts" SDL_CFLAGS="-I$PWD/$prefix/include/SDL" SDL_LIBS="-L$PWD/$prefix/lib -lSDL" &&
make clean && make && make install &&
cd .. &&

rm libiconv-1.13.1 SDL_image-1.2.8 SDL-1.2.14 jpeg-6b libpng-1.2.19 -rf &&

cd ../output/arm_linux/bin &&
echo '#!/bin/sh' > run.sh &&
echo 'export TSLIB_ROOT=$PWD/..' >> run.sh &&
echo 'export TSLIB_TSDEVICE=/dev/input/event0' >> run.sh &&
echo 'export TSLIB_CALIBFILE=$TSLIB_ROOT/etc/pointercal' >> run.sh &&
echo 'export TSLIB_CONFFILE=$TSLIB_ROOT/etc/ts.conf' >> run.sh &&
echo 'export TSLIB_PLUGINDIR=$TSLIB_ROOT/lib/ts' >> run.sh &&
echo 'export TSLIB_CONSOLEDEVICE=none' >> run.sh &&
echo 'export TSLIB_FBDEVICE=/dev/fb0' >> run.sh &&
echo 'export SDL_MOUSEDRV=TSLIB' >> run.sh &&
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TSLIB_ROOT/lib' >> run.sh &&
echo '#ts_test:' >> run.sh &&
echo '$TSLIB_ROOT/bin/ts_calibrate' >> run.sh &&
echo '#$TSLIB_ROOT/bin/ts_harvest' >> run.sh &&
echo '#$TSLIB_ROOT/bin/ts_print' >> run.sh &&
echo '#$TSLIB_ROOT/bin/ts_print_raw' >> run.sh &&
echo '#$TSLIB_ROOT/bin/ts_test' >> run.sh &&
echo '#SDL_test:'>> run.sh &&
echo '#$TSLIB_ROOT/bin/graywin -width 480 -height 272 -bpp 16' >> run.sh &&
echo '#$TSLIB_ROOT/bin/testalpha -width 480 -height 272 -bpp 16' >> run.sh &&
echo '#$TSLIB_ROOT/bin/testgamma -width 480 -height 272 -bpp 16' >> run.sh &&
echo '#$TSLIB_ROOT/bin/testcursor -width 480 -height 272 -bpp 16' >> run.sh &&
chmod +x run.sh &&

cd ../../../.. && rm $tmp -rf && echo "Install Success!"
